package com.fast.user.web.mobile;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fast.base.cache.MemcachedUtils;
import com.fast.base.db.sql.impl.SqlGeneratorImpl;
import com.fast.base.model.AnoSysLog;
import com.fast.base.util.DateUtils;
import com.fast.base.util.LogUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.ResponseEnum;
import com.fast.base.web.BaseController;
import com.fast.cms.constant.CommonConstant;
import com.fast.common.userCache.MemberCacheUtils;
import com.fast.user.constant.LoginTypeEnum;
import com.fast.user.dto.CommonLoginDTO;
import com.fast.user.model.admin.AdminUser;
import com.fast.user.model.member.Member;
import com.fast.user.service.admin.AdminUserService;
import com.fast.user.service.dept.CompanyService;
import com.fast.user.service.member.MemberService;

import io.swagger.annotations.Api;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 *
 * 类名:com.fast.user.web.mobile.AppAdminController <pre>
 * 标题:系统管理员app端接口
 * 编写者:lushuifa
 * 创建时间:2017年12月17日 下午5:18:38
 * </pre>
 */
@Api(value="APP用户控制类",tags={"公共用户操作相关"})
@ApiIgnore
@Controller("appCommonUserController")
@RequestMapping("/app/commonUser")
public class AppCommonUserController extends BaseController{
	
	@Resource(name="adminUserService")
	private AdminUserService adminUserService;
	
	@Resource(name="companyService")
	private CompanyService companyService;
	
	@Resource(name="memberService")
	private MemberService memberService;
	

	/**
	 * 说明:app统一登录入口 <br>
	 * 编写者:lushuifa
	 * 创建时间:2018年4月13日 下午11:09:02 </pre>
	 * @param loginName 登录名：手机号，用户名，邮箱
	 * @param password 密码
	 * @return ResponseData 说明
	 */
	@ResponseBody
	@RequestMapping(value="/login.do",method = RequestMethod.POST)
	@AnoSysLog(actionName="后台登录系统")
	public ResponseData login(
			@RequestParam(required=true) String loginName,
			@RequestParam(required=true) String password,
			@RequestParam(required=true) int loginType){
		LogUtils.info("login_loginName:"+loginName+",password:"+password+",loginType:"+loginType);
		ResponseData responseData = new ResponseData();
		try {
			//管理员登录
			if(loginType==LoginTypeEnum.admin.getCode()) {
				AdminUser adminUser = this.adminUserService.login(loginName, password);
				if(null!=adminUser) {
					CommonLoginDTO commonLoginDto = new CommonLoginDTO();
					commonLoginDto.setLoginName(loginName);
					commonLoginDto.setRealName(adminUser.getRealName());
					commonLoginDto.setLoginType(loginType);
					commonLoginDto.setUserId(adminUser.getUuid());
					responseData.setData(commonLoginDto);
					LogUtils.info("login_admin_result:"+responseData);
					return responseData;
				}else {
					responseData.buildResult(ResponseEnum.USER_NULL);
					LogUtils.info("login_admin_result:"+responseData);
					return responseData;
				}
			}else if(loginType==LoginTypeEnum.member.getCode()) {
				Member member = this.memberService.login(loginName, password);
				if(null!=member) {
					CommonLoginDTO commonLoginDto = new CommonLoginDTO();
					commonLoginDto.setLoginName(loginName);
					commonLoginDto.setRealName(member.getRealName());
					commonLoginDto.setLoginType(loginType);
					commonLoginDto.setUserId(member.getUuid());
					responseData.setData(commonLoginDto);
					LogUtils.info("login_member_result:"+responseData);
					return responseData;
				}else {
					responseData.buildResult(ResponseEnum.USER_NULL);
					LogUtils.info("login_member_result:"+responseData);
					return responseData;
				}
			}else {
				responseData.buildResult(ResponseEnum.FAILURE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("logn.responseData:"+responseData.toString());
		return responseData;
	}
	
	/**
	 * 通过手机验证码登录
	 * @param mobile
	 * @param verifyCode 手机验证码
	 * @return
	 */
	@RequestMapping(value="/smsLogin",method = RequestMethod.POST)
    @ResponseBody
	public ResponseData smsLogin(@RequestParam(required=true) String mobile,
			@RequestParam(required=true) String verifyCode,
			@RequestParam(required=true) int loginType){
		LogUtils.info("memberLogin.mobile:"+mobile+",verifyCode:"+verifyCode+",loginType:"+loginType);
		ResponseData responseData = new ResponseData();
		try {
			String sessionKey = CommonConstant.getMobileSessionKey(mobile);
			//校验手机验证码
			String sessionMobileCode = (String) MemcachedUtils.get(sessionKey);
			if(!StringUtils.equals(sessionMobileCode, verifyCode)){
				responseData.buildResult(ResponseEnum.CAPTCHA_FAILD);
				return responseData;
			}
			
			//管理员登录
			if(loginType==LoginTypeEnum.admin.getCode()) {
				AdminUser adminUser = this.adminUserService.queryAdminInfoByMobile(mobile);
				if(null!=adminUser) {
					CommonLoginDTO commonLoginDto = new CommonLoginDTO();
					commonLoginDto.setLoginName(adminUser.getUserName());
					commonLoginDto.setRealName(adminUser.getRealName());
					commonLoginDto.setLoginType(loginType);
					commonLoginDto.setUserId(adminUser.getUuid());
					responseData.setData(commonLoginDto);
					LogUtils.info("login_admin_result:"+responseData);
					return responseData;
				}else {
					responseData.buildResult(ResponseEnum.USER_NULL);
					LogUtils.info("login_admin_result:"+responseData);
					return responseData;
				}
			}else if(loginType==LoginTypeEnum.member.getCode()) {
				Member member = this.memberService.queryMemberByMobile(mobile);
				if(member.getLocked()==0){
					member.setLoginIp(webUtil.getIpAddr());
					member.setLoginDateTime(DateUtils.getNow());
					member.setLoginCount(member.getLoginCount()+1);
					String updateSql = "update "+SqlGeneratorImpl.getTableName(Member.class)+" set loginIp='"+webUtil.getIpAddr()+"',loginDateTime='"+DateUtils.getNow()+"',loginCount=loginCount+1 where id="+member.getId();
					this.memberService.modifyRow(updateSql);
					//改变登录次数
					int newCount = this.memberService.increaseLoginCount(member.getId());
					member.setLoginCount(newCount);
					if(StringUtils.isNotBlank(member.getHeadImageUrl())){
						member.setHeadImageUrl(getImgPath()+member.getHeadImageUrl());
					}
					CommonLoginDTO commonLoginDto = new CommonLoginDTO();
					commonLoginDto.setLoginName(member.getNickName());
					commonLoginDto.setRealName(member.getRealName());
					commonLoginDto.setLoginType(loginType);
					commonLoginDto.setUserId(member.getUuid());
					responseData.setData(member);
					MemberCacheUtils.saveMember(member);
				} else {
					responseData.buildResult(ResponseEnum.USER_HAS_LOCKED);
				}
			}else {
				responseData.buildResult(ResponseEnum.FAILURE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("smsLogin result:"+responseData);
		return responseData;
	}
}
